package com.service.impl;

import com.dao.MemberDao;
import com.dao.Td_punishedDao;
import com.entity.Td_tblpartyorgmembers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import com.service.inte.MemberServiceInte;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.EntityManagerFactory;
import java.io.*;
import java.util.List;

@Service
public class MemberServiceInteImpl implements MemberServiceInte {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Override
    public List<Td_tblpartyorgmembers> getAllMember() {

        return memberDao.getAllMember();
    }

    @Override
    public List<Td_tblpartyorgmembers> getAllByFd_branchId(int branchId) {
        return memberDao.getAllByFd_branchId(branchId);
    }

    @Override
    public List<Td_tblpartyorgmembers> getAllApplyByBranchId(int type, int branchId) {
        return memberDao.getAllApplyByBranchId(type, branchId);
    }

    @Override
    public List<Td_tblpartyorgmembers> getAllApply(int type) {
        return memberDao.getAllApply(type);
    }

    @Override
    public List<Td_tblpartyorgmembers> getApprovalByBranchId(int type, int branchId) {
        return getApprovalByBranchId(type, branchId);
    }

    @Override
    public List<Td_tblpartyorgmembers> getAllApprroval(int type) {
        return getAllApply(type);
    }

    @Override
    public List<Object[]> getMemberByName(String name) {
        return memberDao.getMemberByName("%" + name + "%");
    }

    @Override
    public List<Td_tblpartyorgmembers> getAllPartyMember() {
        return memberDao.getAllPartyMember();
    }

    @Override
    public Td_tblpartyorgmembers getByLoginId(int loginId) {
        return memberDao.getByFd_loginId(loginId);
    }

    @Override
    public List<Object[]> getMemberByName(String Name, int page) {
        return memberDao.getMemberByName("%" + Name + "%", new PageRequest(page, 10));
    }

    @Override
    public int updateBranch(int branchId, String branchName, int loginId) {
        return memberDao.updateBranchByLoginId(branchId, branchName, loginId);
    }

    @Override
    public List<Object[]> getAllApprroval(int type, String name, int page) {
        return memberDao.getAllApprroval(type, "%" + name + "%", new PageRequest(page, 10));
    }

    @Override
    @Transactional
    public void saveOrUpdate(Td_tblpartyorgmembers td_tblpartyorgmembers) {
        try {
            memberDao.save(td_tblpartyorgmembers);
        } catch (Exception e) {

        }
    }

    @Override
    public String uploadMemberImage(MultipartFile imgName, int loginId) {
        if (!imgName.isEmpty()) {
            String saveFileName = imgName.getOriginalFilename();
            saveFileName = loginId + "-" + saveFileName;
            File saveFile = new File("d:/user/" + saveFileName);

            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            System.out.println("文件路劲：" + saveFile.getParentFile());
            try {
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveFile));
                out.write(imgName.getBytes());
                out.flush();
                out.close();
                return "d:/user/" + saveFileName;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败";
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "缺少文件，上传失败";
        }
    }

    @Override
    @Transactional
    public int updateTransferState(int state, int loginId) {
        return memberDao.updateTransferState(state,loginId);
    }

}
